package com.idp.web.test.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.test.entity.Logistics;
import com.idp.web.test.entity.LogisticsHasWarehouse;
import com.idp.web.test.service.LogisticsHasWarehouseService;
import com.idp.web.test.service.LogisticsService;

import net.sf.json.JSONObject;

/**
 * 
 * 物流和仓库多对多controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-12 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 * 		<pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/logisticsHasWarehouse")
public class LogisticsHasWarehouseController extends BaseController {

	private Logger logger = Logger.getLogger(LogisticsHasWarehouseController.class);

	@Resource
	private LogisticsHasWarehouseService logisticsHasWarehouseService;

	@Resource
	private LogisticsService logisticsService;

	/**
	 * 
	 * <pre>
	 * 	2017-06-12 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init() {

		return "test/logisticsHasWarehouseSearch";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-12 Amber
	 * 	分页查询
	 * </pre>
	 * 
	 * @param logisticsHasWarehouse
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(LogisticsHasWarehouse logisticsHasWarehouse, Page<LogisticsHasWarehouse> page,
			HttpServletRequest request) {

		try {

			request.setAttribute("page", logisticsHasWarehouseService.findByPage(logisticsHasWarehouse, page));
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "test/logisticsHasWarehouseList";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-12 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/logisticsHasWarehouse")
	public String logisticsHasWarehouse(Integer warehouseId, String warehouseName, Model model) {
		System.out.println("==============================================");
		try {
			/*
			 * if(ValidateUtils.isNotEmpty(id)){
			 * System.out.println("-----------------------"+request.getParameter
			 * ("warehouseId")); request.setAttribute("warehouseId",
			 * request.getParameter("warehouseId")); }
			 */
			/*
			 * request.setAttribute("warehouseId",
			 * request.getParameter("warehouseId"));
			 * 
			 * request.setAttribute("warehouseName",
			 * request.getParameter("warehouseName"));
			 * request.setAttribute("logisticses",
			 * logisticsService.findBySearch(new Logistics()));
			 */
			model.addAttribute("warehouseId", warehouseId);
			model.addAttribute("warehouseName", warehouseName);
			model.addAttribute("logisticses", logisticsService.findLeft(warehouseId));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/warehouse/logisticsHasWarehouse";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-12 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param logisticsHasWarehouse
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(LogisticsHasWarehouse logisticsHasWarehouse) {
		JSONObject json = new JSONObject();
		try {
			// 修改
			if (ValidateUtils.isNotEmpty(logisticsHasWarehouse.getId())) {
				logisticsHasWarehouseService.update(logisticsHasWarehouse);
			}
			// 新增
			else {
				logisticsHasWarehouseService.add(logisticsHasWarehouse);
			}
			json.put("result", "save_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}
		json.put("warehouseId", logisticsHasWarehouse.getWarehouseId());
		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-12 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {

		JSONObject json = new JSONObject();

		try {

			logisticsHasWarehouseService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}
		return json.toString();
	}
}
